//
//  Filter.swift
//  Screen
//
//  Created by Natsu on 12/26/16.
//  Copyright © 2016 alex.chih. All rights reserved.
//

import UIKit

class NaFilter: NSObject
{
    @objc
    public static func requestDic(url:String, par: Any?) -> String
    {
        var _par:NSMutableDictionary? = nil
        
        if par != nil
        {
            if par is NSMutableDictionary
            {
                _par = par as! NSMutableDictionary?
            }
            else if par is [String: Int64]
            {
                _par = NSMutableDictionary()
                
                for (key,value) in par as! [String: Int64]
                {
                    _par?[key as String] = String(value)
                }
            }
            else if par is [String: Any]
            {
                _par = NSMutableDictionary()
                
                for (key,value) in par as! [String: Any]
                {
                    _par?[key as String] = value as! String
                }
            }
            else
            {
                fatalError("holy shit, Add some class more")
            }
        }
        
//        let url = "http://www.abc.com/mobile/main/search/searchAll?os=android&pageSize=10&timeSign=2016-12-28 17:57&title=我"
//        _par = nil
        
        var first = true
        var has = false
        var result = ""
        var sign = ""
        let timeSign = DateHelper.CurrentDate(format: "yyyy-MM-dd HH:mm")
        
        var Para = Dictionary<String, String>()
        Para["timeSign"] = timeSign

        var prePara = url.components(separatedBy: "?")
        
        let preRes = prePara[0].components(separatedBy: "//")
        
        if(preRes.count != 2 )
        {
            fatalError("preRes")
        }
        
        var tmp = preRes[1].components(separatedBy: "/")
        tmp.remove(at: 0)
        
        result = "/" + tmp.map{String($0)}.joined(separator: "/")  // join("/", tmp)
        
        //有地址参数
        if prePara.count > 1
        {
            prePara = prePara[1].components(separatedBy: "&")
            
            for str in prePara
            {
                var pair = str.components(separatedBy: "=")
                
                guard pair.count == 2 else {
                    continue
                }
                
                Para[pair[0]] = pair[1]
            }
            
            //first = false
            has = true
        }
        //无地址参数
        else
        {
            
        }
        
        //有字典参数
        if _par != nil
        {
            let keys = _par!.allKeys
            for item : Any in keys
            {
                let key = item as! String
                
                if(_par!.object(forKey: key) is Int64)
                {
                    let tmp:Int64 = _par!.object(forKey: key) as! Int64
                    
                    Para[key] = String(stringInterpolationSegment: tmp)
                }
                else if(_par!.object(forKey: key) is String)
                {
                    Para[key] = _par!.object(forKey: key) as? String
                }
                else
                {
                    fatalError("add more type")
                }
            }
        }
        else
        {
            
        }
        
        let sortedPairs = Para.sorted {$0.0 < $1.0}

        for (key, value) in sortedPairs
        {
            if first
            {
                first = false

                result = result + "?" + key + "=" + value
            }
            else
            {
                result = result + "&" + key + "=" + value
            }
        }
        
        sign = Encrypt.salt(str: result, key: "screen")
        
        result = url + (has ? "&sign=" : "?sign=") + sign + "&timeSign=" + timeSign
        
        result = result.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
        
        return result
        
    }
}
